import { DetailsPage } from './../details/details';
import { RestProvider } from './../../providers/rest/rest';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { ModalController } from 'ionic-angular/components/modal/modal-controller';
import { Component } from '@angular/core';
import { NavController, Tabs } from 'ionic-angular';
import { Storage } from '@ionic/storage'
import { QuestionPage } from '../question/question';
import { BaseUI } from '../../common/baseui';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage extends BaseUI{

  feeds: string[]
  errorMessage: any

  constructor(public navCtrl: NavController,
    public loadingCtrl: LoadingController,
    public rest: RestProvider,
    public modalCtrl: ModalController) {
       super()
  }

  ionViewDidLoad(){
   this.getFeeds()
  }


  gotoQuestion () { 
    let modal =  this.modalCtrl.create(QuestionPage)
    modal.present()
  }

  gotoChat () {
    this.selectTab(2)
  }

  /**
   * 选定指定的tab
   * 
   * @param {number} index 
   * @memberof HomePage
   */
  selectTab(index: number) {
    let t:Tabs = this.navCtrl.parent
    t.select(index)
  }

  getFeeds () {
    let loading = super.showLoading(this.loadingCtrl, '加载中...')
    this.rest.getFeeds()
    .subscribe(f => {
      this.feeds = f
      loading.dismiss() 
    }, error => {
      this.errorMessage = <any>error
    })
  }

  gotoDetails(questionId) {
    // 页面之间的传参
    this.navCtrl.push(DetailsPage, {id: questionId})
  }
}
